
package combat.dudes.client.gui.threads.shell;

import gnu.cajo.utils.extra.TransparentItemProxy;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import combat.dudes.DudesConfig;
import combat.dudes.DudesFunctions;
import combat.dudes.client.ClientApp;
import combat.dudes.client.ClientApp.SharedVars;
import combat.dudes.client.gui.ClientGUI;
import combat.dudes.client.gui.ClientGUIInternalWindow;
import combat.dudes.client.gui.ClientGUITheme;
import combat.dudes.client.gui.threads.ClientThread;
import combat.dudes.protocol.data.DoublePoint;
import combat.dudes.protocol.interfaces.ChatInt;
import combat.dudes.protocol.interfaces.ScenarioInt;
import combat.dudes.protocol.interfaces.SimulationInt;
import combat.dudes.protocol.interfaces.TeamInt;
import combat.dudes.protocol.interfaces.VehicleInt;


public class GUIShellLogin extends ClientGUIInternalWindow {
	private static final long serialVersionUID = 1L;

	public static final int w = 486;
	public static final int h = 530;
	public static final int x = Math.round(Toolkit.getDefaultToolkit().getScreenSize().width / 2 /*- w / 2*/);
	public static final int y = Math.round(Toolkit.getDefaultToolkit().getScreenSize().height / 2 - h / 2);

	private final static JPanel panelBg = new JPanel() {
		private static final long serialVersionUID = 1L;
		@Override
		public void paintComponent(Graphics g) {
			Graphics2D g2 = (Graphics2D) g;
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Set AA on for shapes
			g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); // Set AA off for text
			g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED); // Set render focus on speed

			Image img = ClientGUITheme.Background.bgLogIn;
			g2.drawImage(img, 0, 0, img.getWidth(this), img.getHeight(this), this);
			g2.setFont(new Font("Segoe UI", Font.PLAIN, 11));
			g2.setColor(new Color(150, 150, 150));
			g2.drawString("My IP: " + DudesFunctions.Network.getMyIPAddress(), 28, img.getHeight(this) - 6);
			g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); // Set AA off for text

		}
	};
	private final static JPanel panelFormLogin = new JPanel(null) {
		private static final long serialVersionUID = 1L;
		@Override
		protected void paintComponent(Graphics g) {
			Image img = ClientGUITheme.Background.bgLogIn;
			g.drawImage(img, -64, -275, img.getWidth(this), img.getHeight(this), this);
		}
	};
	private final static JPanel panelFormTeamVehicle = new JPanel(null) {
		private static final long serialVersionUID = 1L;
		@Override
		protected void paintComponent(Graphics g) {
			Image img = ClientGUITheme.Background.bgLogIn;
			g.drawImage(img, -64, -275, img.getWidth(this), img.getHeight(this), this);
		}
	};


	private final static ArrayList<String> servers = SharedVars.getServerlist();

	private static int serverListSelected = 0;
	private final static JRadioButton rbnOtherServer = new JRadioButton() {
		private static final long serialVersionUID = 1L;
		@Override
		protected void paintComponent(Graphics g) {
			g.setColor(new Color(210, 210, 218));
			g.fillRect(0, 0, this.getWidth(), this.getHeight());
			super.paintComponent(g);
		}
	};
	private final static JButton btnSubmit1 = new JButton("Submit") {
		private static final long serialVersionUID = 1L;
		@Override
		protected void paintComponent(Graphics g) {
			g.setColor(new Color(209, 210, 215));
			g.fillRect(0, 0, this.getWidth(), this.getHeight());
			super.paintComponent(g);
		}
	};
	public GUIShellLogin() {
		super(-1, "Log in", x, y, w, h, "lock.png", panelBg);
		this.setClosable(false);

		// Form header
		/* JLabel header = new JLabel("Not logged in");
		 * header.setFont(new Font("Segoe UI", Font.PLAIN, 14));
		 * header.setHorizontalAlignment(JLabel.LEFT);
		 * header.setVerticalAlignment(JLabel.TOP);
		 * header.setBackground(new Color(0, 0, 0, 0));
		 * panelBg.add(header);
		 * layout.putConstraint(SpringLayout.WEST, header, 60, SpringLayout.WEST, panelBg);
		 * layout.putConstraint(SpringLayout.NORTH, header, 430, SpringLayout.NORTH, panelBg);
		 * layout.putConstraint(SpringLayout.EAST, header, 0, SpringLayout.EAST, panelBg);
		 * layout.putConstraint(SpringLayout.SOUTH, header, 0, SpringLayout.SOUTH, panelBg); */

		panelFormLogin.setBackground(new Color(0, 0, 0, 0));
		SpringLayout layout = new SpringLayout();
		panelBg.setLayout(layout);

		panelBg.add(panelFormLogin);
		panelBg.add(panelFormTeamVehicle);

		layout.putConstraint(SpringLayout.WEST, panelFormLogin, 64, SpringLayout.WEST, panelBg);
		layout.putConstraint(SpringLayout.NORTH, panelFormLogin, 275, SpringLayout.NORTH, panelBg);
		layout.putConstraint(SpringLayout.EAST, panelFormLogin, -50, SpringLayout.EAST, panelBg);
		layout.putConstraint(SpringLayout.SOUTH, panelFormLogin, -20, SpringLayout.SOUTH, panelBg);

		layout.putConstraint(SpringLayout.WEST, panelFormTeamVehicle, 64, SpringLayout.WEST, panelBg);
		layout.putConstraint(SpringLayout.NORTH, panelFormTeamVehicle, 275, SpringLayout.NORTH, panelBg);
		layout.putConstraint(SpringLayout.EAST, panelFormTeamVehicle, -20, SpringLayout.EAST, panelBg);
		layout.putConstraint(SpringLayout.SOUTH, panelFormTeamVehicle, -20, SpringLayout.SOUTH, panelBg);

		FormLogin.show();

		this.add(panelBg);
		panelBg.setVisible(true);
		this.setVisible(true);
	}

	private static class FormLogin {

		public static void show() {

			// Username
			JLabel lblUsername = new JLabel("Username", JLabel.RIGHT);
			lblUsername.setFont(ClientGUITheme.guiFont);
			lblUsername.setBounds(0, 0, 125, 30);
			final JTextField txtUsername = new JTextField(10) {
				private static final long serialVersionUID = 1L;
				@Override
				protected void paintComponent(Graphics g) {
					super.paintComponent(g);
					g.drawImage(DudesFunctions.File.getIcon("user.png"), 5, 5, this);
					setMargin(new Insets(-3, 20, -3, 2));
				}
			};
			txtUsername.setBounds(150, 2, 125, 26);
			txtUsername.setFont(ClientGUITheme.guiFont);
			txtUsername.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent ke) {
					int max = 25;
					if(txtUsername.getText().length() > max - 1)
						txtUsername.setText(txtUsername.getText().substring(0, max - 1));
					// Enter key pressed
					if(ke.getKeyCode() == KeyEvent.VK_ENTER) btnSubmit1.doClick();
				}
			});

			// Password
			JLabel lblPassword = new JLabel("Password", JLabel.RIGHT);
			lblPassword.setFont(ClientGUITheme.guiFont);
			lblPassword.setBounds(0, 31, 121, 22);
			final JPasswordField txtPassword = new JPasswordField(10) {
				private static final long serialVersionUID = 1L;
				@Override
				protected void paintComponent(Graphics g) {
					super.paintComponent(g);
					g.drawImage(DudesFunctions.File.getIcon("key.png"), 5, 5, this);
					setMargin(new Insets(-3, 20, -3, 2));
				}
			};
			txtPassword.setBounds(150, 28, 125, 26);
			txtPassword.setFont(ClientGUITheme.guiFont);
			txtPassword.addKeyListener(new KeyListener() {
				@Override
				public void keyPressed(KeyEvent ke) {
					String passwordString = "";
					char[] passwordInput = txtPassword.getPassword();
					for(int i = 0; i < passwordInput.length; i++)
						passwordString += "" + passwordInput[i];
					if(passwordInput.length > 25)
						txtPassword.setText(passwordString.substring(0, 24));

					// Enter key pressed
					if(ke.getKeyCode() == KeyEvent.VK_ENTER) btnSubmit1.doClick();
				}
				@Override
				public void keyTyped(KeyEvent ke) {}
				@Override
				public void keyReleased(KeyEvent ke) {}
			});

			// Server
			JLabel lblServerlist = new JLabel("Server", JLabel.RIGHT);
			lblServerlist.setFont(ClientGUITheme.guiFont);
			lblServerlist.setBounds(0, 58, 121, 22);

			final ButtonGroup serverBtn = new ButtonGroup();
			for(final String server : servers) {
				JRadioButton radio = new JRadioButton(server);
				if(servers.indexOf(server) == 0) radio.setSelected(true);

				radio.addItemListener(new ItemListener() {
					@Override
					public void itemStateChanged(ItemEvent ie) {
						if(ie.getStateChange() == ItemEvent.SELECTED) serverListSelected = servers.indexOf(server);
					}
				});
				radio.setFont(ClientGUITheme.guiFont);
				radio.setBounds(158, 55 + 28 * servers.indexOf(server), 150, 28);
				serverBtn.add(radio);
				panelFormLogin.add(radio);
			}

			rbnOtherServer.addItemListener(new ItemListener() {
				@Override
				public void itemStateChanged(ItemEvent ie) {
					if(ie.getStateChange() == ItemEvent.SELECTED) serverListSelected = -1;
				}
			});
			serverBtn.add(rbnOtherServer);
			final JTextField txtOtherServer = new JTextField(10) {
				private static final long serialVersionUID = 1L;
				@Override
				protected void paintComponent(Graphics g) {
					super.paintComponent(g);
					g.drawImage(DudesFunctions.File.getIcon("server.png"), 5, 5, this);
					setMargin(new Insets(-3, 20, -3, 0));
				}
			};
			txtOtherServer.addFocusListener(new FocusListener() {
				@Override
				public void focusGained(FocusEvent fe) {
					rbnOtherServer.setSelected(true);
				}
				@Override
				public void focusLost(FocusEvent fe) {}
			});
			txtOtherServer.addKeyListener(new KeyListener() {
				@Override
				public void keyPressed(KeyEvent ke) {
					int max = 15;
					if(txtOtherServer.getText().length() > max)
						txtOtherServer.setText(txtOtherServer.getText().substring(0, max - 1));

					// Enter key pressed
					if(ke.getKeyCode() == KeyEvent.VK_ENTER) btnSubmit1.doClick();
				}
				@Override
				public void keyTyped(KeyEvent ke) {}
				@Override
				public void keyReleased(KeyEvent ke) {}
			});
			rbnOtherServer.setBounds(158, 58 + 28 * servers.size(), 26, 26);
			txtOtherServer.setBounds(178, 58 + 28 * servers.size(), 130, 26);
			txtOtherServer.setFont(ClientGUITheme.guiFont);

			panelFormLogin.add(txtOtherServer);
			panelFormLogin.add(rbnOtherServer);


			// Submit button
			btnSubmit1.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent ae) {
					if(ae.getActionCommand().equalsIgnoreCase("Submit")) {

						// Validate entered data

						if(txtUsername.getText().isEmpty()) {
							DudesFunctions.Status.showAlert(GUIShell.windowLogin,
									"Username empty",
									"Please fill in your username.",
									JOptionPane.WARNING_MESSAGE, "error.png");
						} else if(serverListSelected == -1 && txtOtherServer.getText().isEmpty()) {
							DudesFunctions.Status.showAlert(GUIShell.windowLogin,
									"Server IP empty",
									"You've chosen the 'other' option, but left the IP-address field empty.\nPlease provide the IP-address of the server.",
									JOptionPane.WARNING_MESSAGE, "error.png");
						} else if(serverListSelected == -1 && !DudesFunctions.Network.validateIP(txtOtherServer.getText())) {
							DudesFunctions.Status.showAlert(GUIShell.windowLogin,
									"Server IP invalid",
									"The IP-address you filled in is not correct.\nPlease fill in a valid IP-address for the server.",
									JOptionPane.WARNING_MESSAGE, "error.png");
						} else {
							String server = "";
							if(serverListSelected == -1)
								server = txtOtherServer.getText();
							else server = servers.get(serverListSelected);
							// String[] data = { server, txtUsername.getText(), (String)txtPassword.getPassword() };
							String[] data = { server, txtUsername.getText() };
							doSubmit(1, data);
						}
					}
				}
			});
			btnSubmit1.setFont(new Font("Segoe UI", Font.PLAIN, 13));
			btnSubmit1.setBounds(120, txtOtherServer.getY() + txtOtherServer.getHeight() + 13, 80, 28);


			panelFormLogin.add(lblUsername);
			panelFormLogin.add(txtUsername);
			panelFormLogin.add(lblPassword);
			panelFormLogin.add(txtPassword);
			panelFormLogin.add(lblServerlist);
			panelFormLogin.add(btnSubmit1);

			panelFormLogin.setVisible(true);
		}


		private static ArrayList<TeamInt> teams = new ArrayList<TeamInt>();
		private static ArrayList<VehicleInt> vehicles = new ArrayList<VehicleInt>();
		private static class FormTeamVehicle {

			private static JComboBox teamList = new JComboBox();
			private static JButton btnRefreshTeams = new JButton("Refresh");

			private static JComboBox vehicleList = new JComboBox();
			private static JButton btnRefreshVehicles = new JButton("Refresh");

			private static int selected = 0;

			public static void show() {
				panelFormLogin.setVisible(false);
				panelFormTeamVehicle.setVisible(true);

				if(SharedVars.sim != null) {

					// Teams
					ClientApp.Log("Trying to retrieve teams from server");
					teams = SharedVars.sim.getTeams();
					if(teams.size() == 0) ClientApp.Log("Number of teams on server equals 0");

					JLabel lblTeam = new JLabel("Team", JLabel.RIGHT);
					lblTeam.setFont(ClientGUITheme.guiFont);
					lblTeam.setBounds(0, 2, 125, 30);

					teamList.removeAllItems();
					for(TeamInt team : teams)
						teamList.addItem(team.getTeamName());

					if(teams.size() > 0) teamList.setSelectedIndex(selected);
					teamList.setBounds(150, 2, 100, 28);
					teamList.setFont(ClientGUITheme.guiFont);
					teamList.setEditable(false);
					teamList.addItemListener(new ItemListener() {
						@Override
						public void itemStateChanged(ItemEvent ie) {
							if(ie.getStateChange() == ItemEvent.SELECTED) {
								selected = teamList.getSelectedIndex();
								btnRefreshVehicles.doClick();
							}
						}
					});

					btnRefreshTeams.addActionListener(new ActionListener() {

						@Override
						public void actionPerformed(ActionEvent ae) {
							if(ae.getActionCommand().equalsIgnoreCase("Refresh")) {
								selected = teamList.getSelectedIndex();
								teams = SharedVars.sim.getTeams();
								teamList.removeAllItems();
								for(TeamInt team : teams)
									teamList.addItem(team.getTeamName());
								if(selected < teams.size()) teamList.setSelectedIndex(selected);

								vehicleList.removeAllItems();
								btnRefreshVehicles.doClick();
							}
						}
					});
					btnRefreshTeams.setBounds(teamList.getX() + teamList.getWidth() + 15, 2, 75, 28);

					// Vehicles
					ClientApp.Log("Getting vehicles for selected team");

					JLabel lblVehicle = new JLabel("Vehicle", JLabel.RIGHT);
					lblVehicle.setFont(ClientGUITheme.guiFont);
					lblVehicle.setBounds(0, 40, 125, 30);

					vehicleList.setBounds(150, 40, 190, 28);
					vehicleList.setFont(ClientGUITheme.guiFont);
					vehicleList.setEditable(false);

					final JButton btnSubmit2 = new JButton("Ready!");
					btnRefreshVehicles.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent ae) {
							if(ae.getActionCommand().equalsIgnoreCase("Refresh")) {
								vehicles = SharedVars.sim.getTeams().get(teamList.getSelectedIndex()).getVehicles();
								vehicleList.removeAllItems();

								for(VehicleInt vehicle : vehicles)
									if(!vehicle.getLocked()) vehicleList.addItem(vehicle.getName());


								if(vehicleList.getItemCount() == 0) {
									btnSubmit2.setEnabled(false);
									vehicleList.addItem("No vehicles found...");
									vehicleList.setEnabled(false);
								} else {
									vehicleList.setEnabled(true);
									vehicleList.setSelectedIndex(0);
									btnSubmit2.setEnabled(true);
								}

								if(selected == 0) btnSubmit2.setEnabled(true);
							}
						}
					});
					btnRefreshVehicles.setBounds(vehicleList.getX() + vehicleList.getWidth() + 15, 40, 75, 28);


					JButton btnLogOut = new JButton("Log out");
					btnLogOut.setFont(new Font("Segoe UI", Font.PLAIN, 13));
					btnLogOut.setBounds(70, 130, 80, 28);
					btnLogOut.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent ae) {
							if(ae.getActionCommand().equalsIgnoreCase("Log out")) {
								doReturn();
							}
						}
					});

					btnSubmit2.setEnabled(false);
					btnSubmit2.setFont(new Font("Segoe UI", Font.BOLD, 13));
					btnSubmit2.setBounds(150, 130, 120, 28);
					btnSubmit2.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent ae) {
							if(ae.getActionCommand().equalsIgnoreCase("Ready!")) {
								String[] data = { Integer.toString(teamList.getSelectedIndex()), vehicleList.getSelectedItem().toString() };
								doSubmit(2, data);
							}
						}
					});


					if(teams.size() > 0) {
						teamList.setSelectedIndex(0);
						btnRefreshVehicles.doClick();
					}

					panelFormTeamVehicle.add(lblTeam);
					panelFormTeamVehicle.add(teamList);
					panelFormTeamVehicle.add(btnRefreshTeams);

					panelFormTeamVehicle.add(lblVehicle);
					panelFormTeamVehicle.add(vehicleList);
					// panelFormTeamVehicle.add(btnRefreshVehicles);

					panelFormTeamVehicle.add(btnLogOut);
					panelFormTeamVehicle.add(btnSubmit2);

				} else doReturn("Showing TeamVehicle form while there's no connection...",
						"Can't find server", "Can't connect with server '" + DudesConfig.serverUrl + "'.\nReturning to login form...");

				panelFormTeamVehicle.setBackground(new Color(0, 0, 0, 0));
			}

			private static void doReturn() {
				panelFormTeamVehicle.setVisible(false);
				SharedVars.logOut();
				panelFormLogin.setVisible(true);
			}
			private static void doReturn(String log, String title, String msg) {
				ClientApp.Log(log);
				DudesFunctions.Status.showAlert(GUIShell.windowLogin, title, msg, JOptionPane.WARNING_MESSAGE, "error.png");
				doReturn();
			}
		}


		private static void doSubmit(int form, String[] data) {
			switch(form) {
				case 1:
					DudesConfig.setServerUrl(data[0]);
					ClientApp.Log("Trying to connect with server '" + DudesConfig.serverUrl + "'");

					Boolean canConnect = false;
					try {
						SharedVars.sim = (SimulationInt) TransparentItemProxy.getItem(DudesConfig.serverUrl + "Simulation", new Class[] { SimulationInt.class });
						canConnect = true;
					} catch(Exception e) {
						ClientApp.onError("Couldn't bind CAJO simulation instance", e, false);
						canConnect = false;
					}

					if(!canConnect) {
						DudesFunctions.Status.showAlert(GUIShell.windowLogin,
								"Can't find server",
								"Can't connect with server '" + data[0] + "'.",
								JOptionPane.WARNING_MESSAGE, "error.png");
					} else {

						ClientApp.Log("Succesfully connected with server!");

						Boolean canConnect2 = false;
						try {
							// SharedVars.sim = (SimulationInt) TransparentItemProxy.getItem(DudesConfig.serverUrl + "Simulation", new Class[] { SimulationInt.class });
							SharedVars.scenario = (ScenarioInt) TransparentItemProxy.getItem(DudesConfig.serverUrl + "Scenario", new Class[] { ScenarioInt.class });
							SharedVars.chat = (ChatInt) TransparentItemProxy.getItem(DudesConfig.serverUrl + "Chat", new Class[] { ChatInt.class });
							canConnect2 = true;
						} catch(Exception e) {
							ClientApp.onError("Couldn't bind remaining CAJO instances", e, false);
							canConnect2 = false;
						}

						if(!canConnect2) {
							DudesFunctions.Status.showAlert(GUIShell.windowLogin,
									"Server connection failed",
									"Server '" + data[0] + "' hasn't activated her 'Scenario' or 'Chat' instance...",
									JOptionPane.WARNING_MESSAGE, "error.png");
						} else {

							// Great success!
							/* char[] passwordInput = txtPassword.getPassword();
							 * if(passwordInput.length > 0) {
							 * String passwordString = "";
							 * for(int i = 0; i < passwordInput.length; i++)
							 * passwordString += "" + passwordInput[i];
							 * if(SharedVars.logIn(txtUsername.getText(), DudesFunctions.MD5.crypt(passwordString))) { */

							if(SharedVars.logIn(data[1])) {
								FormTeamVehicle.show();
							} else {
								DudesFunctions.Status.showAlert(GUIShell.windowLogin,
										"Log on failed",
										"Failed to log in with username '" + data[1] + "'",
										JOptionPane.WARNING_MESSAGE, "error.png");
							}

						}

					}

					break;


				case 2:
					Boolean canConnect3 = false;
					try {
						Boolean isRunning = SharedVars.sim.getIsRunning();
						canConnect3 = true;
					} catch(Exception e) {
						ClientApp.onError("Couldn't see if simulation is running", e, false);
						canConnect3 = false;
					}

					if(!canConnect3) {
						DudesFunctions.Status.showAlert(GUIShell.windowLogin,
								"Can't find server",
								"Can't connect with server '" + data[0] + "' when checking if the simulation was running.",
								JOptionPane.WARNING_MESSAGE, "error.png");
					} else {

						// Set team
						Object teamz0rz = TransparentItemProxy.getItem(SharedVars.sim.getTeams_Remote().get(Integer.parseInt(data[0])), new Class[] { TeamInt.class });
						Object myTeamRemote = SharedVars.sim.switchTeam_Remote(ClientApp.SharedVars.getMe(), (TeamInt) teamz0rz);
						// TeamInt myTeam = (TeamInt) TransparentItemProxy.getItem(myTeamRemote, new Class[] { TeamInt.class });

						// Object myTeamRemote = SharedVars.sim.switchTeam_Remote(SharedVars.getMe(), teams.get(Integer.parseInt(data[0])));
						// TeamInt myTeam = (TeamInt) TransparentItemProxy.getItem(myTeamRemote, new Class[] { TeamInt.class });
						SharedVars.setMyTeam();

						// Set vehicle
						for(Object vehicleRemote : SharedVars.getMyTeam().getVehicles_Remote()) {
							VehicleInt vehicle = (VehicleInt) TransparentItemProxy.getItem(vehicleRemote, new Class[] { VehicleInt.class });
							if(vehicle.getName().equalsIgnoreCase(data[1])) {
								// VehicleInt myVehicle = (VehicleInt) ClientApp.SharedVars.sim.findPhysicalObject(vehicle);
								// VehicleInt myVehicle = (VehicleInt) TransparentItemProxy.getItem(myVehicleRemote, new Class[] { VehicleInt.class });

								ClientApp.SharedVars.getMe().setUserVehicle(vehicle);
								Object myVehicleRemote = ClientApp.SharedVars.getMe().getUserVehicle_Remote();

								VehicleInt myVehicle = (VehicleInt) TransparentItemProxy.getItem(myVehicleRemote, new Class[] { VehicleInt.class });
								myVehicle.orderMove(4, new DoublePoint(75, 75), 100.0);

								SharedVars.setMyVehicle();
								ClientApp.Log("Set my vehicle to '" + SharedVars.getMyVehicle().getName() + "'");
							}
						}

						/* if(SharedVars.getMyVehicle() == null) {
						 * ClientApp.Log("Couldn't find vehicle '" + data[1] + "'");
						 * DudesFunctions.Status.showAlert(GUIShell.windowLogin,
						 * "Can't find vehicle",
						 * "Couldn't find vehicle '" + data[1] + "' for you. Please try again.",
						 * JOptionPane.WARNING_MESSAGE, "error.png");
						 * } else { */
						// DONE! :D
						GUIShell.windowLogin.setVisible(false);


						for(ClientThread thread : ClientGUI.gui_threads_waiting) {
							synchronized(thread) {
								thread.notify();
							}
						}

						for(ClientGUIInternalWindow window : GUIShell.windows)
							window.setVisible(true);
						break;

						// }
					}

			}
		}
	}
}
